package com.sk.bilibili.view.activity;

import static com.sk.bilibili.utils.StringUtils.str;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListAdapter;
import android.widget.TextView;

import com.sk.bilibili.R;
import com.sk.bilibili.bean.Appraise;
import com.sk.bilibili.callBack.TextChangeCallBack;
import com.sk.bilibili.databinding.ActivityAppraiseBinding;
import com.sk.bilibili.model.activity.AppraiseModel;

public class AppraiseActivity extends BaseActivity<ActivityAppraiseBinding, AppraiseModel> {

    public static void start(Context context,Integer themId){
        Intent intent = new Intent(context, AppraiseActivity.class);
        intent.putExtra("themId",themId);
        context.startActivity(intent);
    }

    private Integer themId;
    private Appraise appraise;

    @Override
    protected void afterSetView() {
        themId= (Integer) getIntent().getSerializableExtra("themId");
        model.loadAppraise(themId);
        bindEvent();
    }

    private void bindEvent() {
        bind.ivBack.setOnClickListener(v->finish());
        bind.tvPub.setOnClickListener(v -> {
            appraise.setThemId(themId);
            appraise.setContent(str(bind.etSend));
            appraise.setScore((int) (bind.rbShow.getRating()*2));
            model.addOrUpdate(appraise);
        });
        bind.etSend.addTextChangedListener(new TextChangeCallBack() {
            @Override
            public void afterTextChanged(Editable s) {
                bind.tvInfo.setText(String.format("%d/100",s.length()));
            }
        });
    }

    public void loadAppraiseSuccess(Appraise appraise) {
        if(appraise!=null){
            bind.tvPub.setText("更新");
            bind.rbShow.setRating(appraise.getScore()/2f);
            bind.etSend.setText(appraise.getContent());
        }else {
            appraise=new Appraise();
        }
        this.appraise=appraise;
    }

    public void addSuccess() {
        toast("添加成功");
        finish();
    }

    public void updateSuccess() {
        toast("更新成功");
        finish();
    }

}